package com.wex.app.tools;

import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.eventhandler.EventRunner;

import java.io.RandomAccessFile;

public class CodecButtonWorker {

    public CodecButtonWorker(Button b, LogUtils l, String rFile, String wFile, String info) {
        btn = b;
        log = l;
        readFile = rFile;
        writeFile = wFile;
        id = info;
        codec = new CodecUtils();
        action = new UiActions(EventRunner.getMainEventRunner());

        btn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (btn.getAlpha() > 0.75f) {
                    int ret = codec.start();
                    if (ret != 0 && log != null)
                        log.appendAll(id + " - codec.start(): failed " + ret + "\n");
                    if (ret == 0)
                        btn.setAlpha(0.5f);
                }
                else {
                    codec.stop();
                    btn.setAlpha(1.0f);
                }
            }
        });
    }

    protected void finalize() throws Throwable {
        codec.stop();
    }

    public boolean setCodec(CodecUtils.Type t, CodecUtils.Format f, int width, int height, int fps) {
        return codec.create(t, f, width, height, fps, new CodecUtils.Callback() {
            @Override
            public int usrWriteSysRead(byte[] d) {
                int ret = d.length;
                if (fileRead != null)
                    ret = FileTools.read(fileRead, d);
                //统计
                resultPushFrame += 1;
                resultPushBytes += ret;
                return ret;
            }

            @Override
            public void usrReadSysWrite(byte[] d) {

                //hex格式打印数据
                if (log != null) {
                    if (d.length > 9) {
                        log.append(id + " - usrReadSysWrite: size/" + d.length + " "
                                + Integer.toHexString(d[0] & 0xFF) + " "
                                + Integer.toHexString(d[1] & 0xFF) + " "
                                + Integer.toHexString(d[2] & 0xFF) + " "
                                + Integer.toHexString(d[3] & 0xFF) + " "
                                + Integer.toHexString(d[4] & 0xFF) + " "
                                + Integer.toHexString(d[5] & 0xFF) + " "
                                + Integer.toHexString(d[6] & 0xFF) + " "
                                + Integer.toHexString(d[7] & 0xFF) + " "
                                + Integer.toHexString(d[8] & 0xFF) + " "
                                + Integer.toHexString(d[9] & 0xFF) + " "
                                + "\n");
                    } else
                        log.append(id + " - usrReadSysWrite: size/" + d.length + "\n");
                }

                //写文件
                if (fileWrite != null)
                    FileTools.write(fileWrite, d);

                //统计
                resultReadFrame += 1;
                resultReadBytes += d.length;
                //达到一定帧数后自动关闭
                if (resultReadFrame >= resultReadFrameLimit) {
                    //采样
                    for (int i = 0; i < resultReadSample.length && i < d.length; i++)
                        resultReadSample[i] = d[i];
                    //结束
                    codec.stop();
                }
            }

            @Override
            public void onStart() {
                fileRead = FileTools.openReader(btn.getContext(), readFile);
                if (fileRead == null)
                    log.appendAll("未找到文件: /Android/media/com.wex.app/" + readFile + "\n");
                fileWrite = FileTools.openWriter(btn.getContext(), writeFile);
                if (fileWrite == null)
                    log.appendAll("未找到文件: /Android/media/com.wex.app/" + writeFile + "\n");

                log.appendAll(id + " - onStart\n");

                resultReset();
            }

            @Override
            public void onStop() {
                resultPrint();

                FileTools.close(fileRead);
                FileTools.close(fileWrite);
                fileRead = null;
                fileWrite = null;

                log.appendAll(id + " - onStop\n");

                action.action(new Runnable() {
                    @Override
                    public void run() {
                        btn.setAlpha(1.0f);
                    }
                });
            }
        }) == 0 ? true : false;
    }

    private Button btn;
    private LogUtils log;
    private CodecUtils codec;
    private String readFile;
    private String writeFile;
    private RandomAccessFile fileRead = null;
    private RandomAccessFile fileWrite = null;
    private String id;
    private UiActions action;

    // ---------- 统计 ----------

    private int resultPushBytes = 0;
    private int resultPushFrame = 0;
    private int resultReadBytes = 0;
    private int resultReadFrame = 0;
    private long resultMsBegin = 0;
    private long resultMsEnd = 0;
    private byte[] resultReadSample = new byte[10];
    final private int resultReadFrameLimit = 100; //达到该输出帧数时自动停止

    private void resultReset() {
        resultPushBytes = resultPushFrame = 0;
        resultReadBytes = resultReadFrame = 0;
        resultMsBegin = resultMsEnd = System.currentTimeMillis();
        for (int i = 0; i < resultReadSample.length; i++)
            resultReadSample[i] = 0;
    }

    private void resultPrint() {
        if (log == null)
            return;
        resultMsEnd = System.currentTimeMillis();
        String info = "\n" + id + " - result: ****************************************\n"
                + "  pushFrame: " + resultPushFrame + "  pushBytes: " + resultPushBytes + "\n"
                + "  readFrame: " + resultReadFrame + "  readBytes: " + resultReadBytes + "\n"
                + "  readSample(hex): "
                + Integer.toHexString(resultReadSample[0] & 0xFF) + " "
                + Integer.toHexString(resultReadSample[1] & 0xFF) + " "
                + Integer.toHexString(resultReadSample[2] & 0xFF) + " "
                + Integer.toHexString(resultReadSample[3] & 0xFF) + " "
                + Integer.toHexString(resultReadSample[4] & 0xFF) + " "
                + Integer.toHexString(resultReadSample[5] & 0xFF) + " "
                + Integer.toHexString(resultReadSample[6] & 0xFF) + " "
                + Integer.toHexString(resultReadSample[7] & 0xFF) + " "
                + Integer.toHexString(resultReadSample[8] & 0xFF) + " "
                + Integer.toHexString(resultReadSample[9] & 0xFF) + "\n"
                + "  timeBegin: " + resultMsBegin + "ms  timeEnd: " + resultMsEnd + "ms \n"
                + "  timeInterval: " + (resultMsEnd - resultMsBegin) + "ms \n"
                + "************************************************ \n\n";
        log.appendAll(info);
    }
}
